<?php

/**
 * Class to generate file upload XHTML
 * 
 */

 class UserFileView{
		
	const FileName ="file";
	const submit = "Filesubmit";
	
	private $message = "";
	
	public function __construct(){
		if(isset($_SESSION[NavigationView::FileUploadMessageCode])){
			$message = $this->generateUploadFeedback($_SESSION[NavigationView::FileUploadMessageCode]);
			unset($_SESSION[NavigationView::FileUploadMessageCode]);
		}
	}
 	
	/**
	 * Public function to generate Fileupload form
	 * @param -
	 * @return HTML
	 */
	public function getXHTMLFileUploadForm(){
		return "
		<div class='fileuploaddiv'>
			<h2>Upload an image to your Gallery!</h2>
			<p><strong>Note!</strong> At this point we only accept jpeg, jpg, gif or png files <strong>Note!</strong></p>
			$this->message
			<form action='' method='post' enctype='multipart/form-data'>
				<fieldset>
				<legend><strong>Upload File</strong></legend>
					<label for='file'>Filename:</label>
						<input type='file' name='".self::FileName. "' id='file' /><br />
						<input type='submit' name='".self::submit."' value='Submit' /><br />
				</fieldset>
			</form>
		</div>";
	}
	
	/**
	 * public function to get the file uploaded by the user
	 * @param -
	 * @return File
	 */
	public function GetFile(){
		return $_FILES[self::FileName];
	}
	
	/**
	 * Public function to generate a Unordered list with images uploaded files from user
	 * @param array with filenames and Bool $owner to indicate if we own the images
	 * @return HTML
	 */
	public function getXHTMLFilesList($fileNameArray, $owner){
		$returnString =  "
		<div class='fileListDiv' id='listDiv'>
			<h2>Gallery</h2>
				<ul>";
		 
		 foreach ($fileNameArray as $key => $file) {
			 $returnString .= "
			 		<li class='imgLink'>
			 			<a href='' class='fileListA'><img alt='x' src='UploadedFiles/" . $key . "'/></a>";
					if($owner){
						$returnString .= "<form method='post' action=''>
							<input class='hiddenRadio' type='radio' name='".NavigationView::RemoveThisImage."' value='".$key."'  checked='checked'/>
							<input class='removeImgSubmit' type='submit' name='".NavigationView::removeImgClicked."' value='Remove' />
						</form>";
					}	
			 $returnString .="</li>";
		 }
			 
		$returnString .= "</ul>
			<div class='clear'></div>
		</div>";
		
		return $returnString;
	}
	
	/**
	 * function to check if user tried to upload a file
	 * @access public
	 * @param -
	 * @return bool
	 */
	public function didUserUploadFile(){
		//Has input been given?
    	if (isset( $_POST[self::submit]) == true) {
	      	return true;
    	} 
    	return FALSE;
	}
	
	/**
	 * function to check if user tried to remove a file
	 * @access public
	 * @param -
	 * @return bool
	 */
	public function didUserTryToRemoveFile(){
		if(isset($_POST[NavigationView::removeImgClicked])){
			return true;
		}
		return false;
	}
	
	/**
	 * function to get img name to remove
	 * @access public
	 * @param -
	 * @return string img name
	 */
	public function getUserRemovedFileName(){
		if(isset($_POST[NavigationView::RemoveThisImage])){
			return $_POST[NavigationView::RemoveThisImage];
		}
		return false;
	}
	
	/**
	 * Public function to generate output after fileupload attempt
	 * @param int generated by the fileupload handler
	 * @return HTML - p containing the message
	 */
	public function generateUploadFeedback($int){
		switch ($int) {
			case UserFileHandler::UPLOAD_OK:
				$this->message = "<p class='noerrorP'>File is uploaded and should be visible below!</p>";
				Break;
				
			case UserFileHandler::FILE_EXIST:
				$this->message = "<p class='fileErrorP'>File Exists!</p>";
				Break;
				
			case UserFileHandler::INVALID_FILE:
				$this->message = "<p class='fileErrorP'>Invalid File!</p>";
				Break;
				
			case UserFileHandler::UPLOAD_ERROR:
				$this->message = "<p class='fileErrorP'>Bad file man!!</p>";
				Break;
			
			default:
				return "";
		}
	}
	
	/**
	 * Function to generate remove file feedback
	 * @access public
	 * @param bool
	 * @return html
	 */
	public function generateRemoveFeedback($result){
		if($result){
			$this->message = "<p class='noerrorP'>File was removed!</p>";
		}
		else{
			$this->message = "<p class='fileErrorP'>File was not removed!</p>";
		}
	}
	
	/**
	 * Function to render output when we visit our Homepage 
	 * @param array with filenames
	 * @return HTML
	 */
	public function generateOwnerOutput($fileArray){
		//Generate upload form and uploadfeedback
		$body = $this->getXHTMLFileUploadForm();
		
		//Generate list from file-array
		$body .= $this->getXHTMLFilesList($fileArray, true);
		
		return $body;
	}
	
	/**
	 * Function to render out put when we visit someone elses home 
	 * @param array with filenames
	 * @return HTML
	 */
	public function generateActiveUsersOutput($fileArray){
		//Generate list from file-array, no upload form
		return $this->getXHTMLFilesList($fileArray, false);
	}

 }
